<?php
namespace Admin\Controller;
use Think\Controller;
class VideosController extends BaseController {
   public function videoList(){
        $videosModel=M('videos'); //实例化模型对象 user 表名
		$videos=$videosModel->order('vid desc')->select(); //查询
		$this->videos=$videos; //volist name
		// dump ($users); 
		$this->display();
}
 public function delvideo(){
        // dump($_GET);
        $videoModel=M('videos');
        $vid=$_GET['vid'];
        //var_dump($uid);
        $res=$videoModel->where("vid=$vid")->find();
        //var_dump($res);
        $photoPath=realpath("./Public/Uploads/videophoto".$res['photo']);
        unlink($photoPath);
        if ($videoModel->where("vid=$vid")->delete()) {
        	$this->success('删除成功');
        }else{
        	$this->error('删除失败');
        }

    }
public function add(){   
  if ($_FILES['files']['error']==0) {
                $loginfo=$this->MyUpload();
                $pname=$loginfo['file']['savename'];
                dump ($loginfo);
                

            }
            else{
                $pname="defalt.jpg";
            }
        $videos = D('videos'); 
 
        if ($videos->create()) {
          
                  $data['videoname'] = I('post.videoname');
/*trim('post.videoname');*/
            $data['intro'] = I('post.intro');
            $data['uploadaddress'] =I('post.uploadaddress');
            $data['videoarea'] = I('post.videoarea');
            $data['uploadtime'] =I('post.uploadtime');
            $data['createtime']=date('Y-m-d H:i:s');
            $data['pic']=$pname;
            if (false !== $videos->add($data)){
                 echo $videos->getLastSql();
                 // exit();
                $this->success('添加成功！', 'videoList');
            } else {         $this->error('数据错误');           }
            }else{
            $this->error($videos->getError());}

        }//register end
     public function editvideos(){
      if ($_FILES['files']['error']==0) {
                $loginfo=$this->MyUpload();
                $pname=$loginfo['pic']['savename'];
                dump ($loginfo);
                

            }
            else{
                $pname="defalt.jpg";
            }
          $videos = D('videos'); 
 
        if ($videos->create()) {
          $vid=I('post.id');
           $data['videoname'] = I('post.videoname');
            $data['intro'] = I('post.intro');
            $data['uploadaddress'] =I('post.uploadaddress');
            $data['videoarea'] = I('post.videoarea');
            $data['uploadtime'] =I('post.uploadtime');
            $data['pic']=$pname;
            dump($vid);
            dump($data);
            if (false !== $videos->where("vid=$vid")->save($data)){
                 echo $videos->getLastSql();
                $this->success('修改成功！', 'videoList');
            } else {         $this->error('数据错误');           }
            }else{
            $this->error($videos->getError());}

        }//register end
public function editvideo(){    
  $videos = M('videos'); 
   $vid=$_GET['id'];
   $res=$videos->where("vid=$vid")->find();
   $this->videos=$res;
         
        $this->display();
        }



    public function MyUpload(){
    $upload = new \Think\Upload();// 实例化上传类
    $upload->maxSize   =     3145728 ;// 设置附件上传大小
    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
    $upload->rootPath  =     './Public/Uploads/videophoto/'; // 设置附件上传根目录
    $upload->autoSub   = false;//关闭子目录，默认ture
    //$info=$upload->uploadOne($_FILES['file']);  //上传文件

    //$upload->savePath  =     ''; // 设置附件上传（子）目录
    $upload->savename = 'time';

    $info   =   $upload->upload();  // 上传文件 
    //$dump(info);
    if(!$info) {        $this->error($upload->getError());    }
    else{return $info;}
    
    // else{        $this->success('上传成功！');    }  


    } //MyUpload end

}